import js from "@eslint/js";
import globals from "globals";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";

export default [
  { ignores: ["dist"] },
  {
    files: ["**/*.{js,jsx}"],
    languageOptions: {
      ecmaVersion: 2020,
      globals: globals.browser,
      parserOptions: {
        ecmaVersion: "latest",
        ecmaFeatures: { jsx: true },
        sourceType: "module",
      },
    },
    plugins: {
      "react-hooks": reactHooks,
      "react-refresh": reactRefresh,
    },
    rules: {
      ...js.configs.recommended.rules,
      ...reactHooks.configs.recommended.rules,
      "react-refresh/only-export-components": [
        "warn",
        { allowConstantExport: true },
      ],
      // 代码风格
      indent: ["error", 2, { SwitchCase: 1 }],
      quotes: [
        "error",
        "single",
        { avoidEscape: true, allowTemplateLiterals: true },
      ],
      semi: ["error", "always"],
      "keyword-spacing": ["error", { before: true, after: true }],
      // 最佳实践
      "no-var": "error",
      "prefer-destructuring": ["error", { object: true, array: true }],
      "default-param-last": "error",
      // React 相关规则
      "react/jsx-indent": ["error", 2],
      "react/jsx-quotes": ["error", "prefer-single"],
      "react/jsx-pascal-case": "error",
      "react/jsx-no-undef": "error",
      // 导入规则
      "import/order": [
        "error",
        {
          groups: ["builtin", "external", "internal"],
          "newlines-between": "always",
        },
      ],
      "import/no-cycle": "error",
      // 其他规则
      "no-console": "warn",
      "no-debugger": "error",
      "no-unused-vars": [
        "error",
        {
          vars: "all",
          args: "none",
          ignoreRestSiblings: true,
          varsIgnorePattern: "^[A-Z_]",
        },
      ],
    },
  },
];
